// $(function () {
//     findById();
// });

function findById() {
    let id = sessionStorage.getItem("productId");
    $.ajax({
        url: '/back/product/findById',
        data: {id: id},
        dataType: 'json',
        type: 'get',
        success: function (data) {
            // console.log(data);
            setProductData(data.model);
        }
    })
}

function setProductData(data) {
    console.log(data);
    $("#cuisineName").val(data.cuisineId);
    $("#productName").val(data.name);
    $("#normalPrice").val(data.normalPrice);
    $("#price").val(data.price);
    $("#imgHref").attr("src", data.imgHref);
    $("#cuisineName").val(data.cuisineId);
    $("input[type=checkbox][name=isRecommend]").attr("checked", data.isRecommend == 1 ? true : false);
    $("input[type=checkbox][name=isShow]").attr("checked", data.isShow == 1 ? true : false);
    editor.txt.html(data.content);
}

const E = window.wangEditor;
const editor = new E('#content')
// 或者 const editor = new E( document.getElementById('div1') )
editor.create()

// console.log(editor);
function save() {
    let data = new FormData();
    // data={key:value,key:value}
    data.append("cuisineId", $("#cuisineName").val());
    data.append("productName", $("#productName").val());
    data.append("normalPrice", $("#normalPrice").val());
    data.append("price", $("#price").val());
    data.append("file", $("#file")[0].files[0]);
    data.append("isRecommend", $("input[name=isRecommend]:checked").val());
    data.append("isShow", $("input[name=isShow]:checked").val());
    data.append("content", editor.txt.html());
    data.append("id", sessionStorage.getItem(productId));
    console.log(data);
    $.ajax({
        url: '/back/product/add',
        data: data,
        type: 'post',
        dataType: 'json',
        processData: false,
        contentType: false,
        success: function (data) {
            if (data.result == 'success') {
                $(".right").load("/html/product/productManage.html")
            } else {
                alert("新增失败");
            }
        }
    })
}